#define _DEFAULT_SOURCE

#include "apue.h"
#include <stdlib.h>
#include <unistd.h>

void test(int );

int globval= 6; /* external variable in initialized data */

int main(void) {
    int var;    /* automatic variable on the stack */
    var= 88;
    printf("parent pid is %d\n", getpid());

    test(var);
    _exit(0);
    /* parent continues here */
    // printf("pid = %d, glob = %d, var= %d\n", getpid(), globval, var);
    
    _exit(0);
}

void test(int var) {
    int lc_var= 1000;
    pid_t pid;

    printf("before vfork\n");   /*we don't flush stdio*/
    if ((pid= vfork()) < 0) {
        err_sys("vfork error");
    } else if (pid == 0) {
        // sleep(2);
        var++;
        globval++;          /* child */
        lc_var++;
        return;
    }
    /* parent continues here */
    printf("function test: pid =  %d, glob = %d, var = %d, lc_var= %d\n", getpid(),  globval, var, lc_var);
}
